var EumPkGroup = require('../../Enum/EumPkGroup');
var PkCardTool = require('../../FindTool/PkCardTool');
var AIGetOne = require('./AIGetOne');
var AIGetPairs = require('./AIGetPairs');
var AIGetThree = require('./AIGetThree');
var AIGetShunZi = require('./AIGetShunZi');
var AIGetBomb = require('./AIGetBomb');
var AIGetBombKing = require('./AIGetBombKing');
var FindShunZiTool = require('../../FindTool/FindShunZiTool');


var FindSameCardValueTool = require('../../FindTool/FindSameCardValueTool');
var _ = require('../../../FrameWork/we_underscore');

/**ai要牌 */
var AIGetCard = module.exports = {};

AIGetCard.isAllOK = function(_lastCardInfo, _currChoiceCardsInfo) {
    var isOk = this.isOK(_lastCardInfo, _currChoiceCardsInfo);
    if (true == isOk) {
        return true;
    }

    var cardCnt = _.size(_currChoiceCardsInfo)
    var cardGroupType = _lastCardInfo.type;
    //再查查是不是炸弹
    if (cardGroupType < EumPkGroup.BOMB) {
        if (4 == cardCnt) {
            var sameCardsList = FindSameCardValueTool.check(_currChoiceCardsInfo);
            sameCardsList = _.filter(sameCardsList, function(_cards) {
                return 4 == _.size(_cards);
            });
            if (1 == _.size(sameCardsList)) {
                return true;
            }
        }
    }
    if (cardGroupType < EumPkGroup.BOMBKING) {
        if (2 == cardCnt) {
            var kingCards = _.filter(_currChoiceCardsInfo, function(_cardInfo) {
                return 5 == _cardInfo.cardType;
            });
            if (2 == _.size(kingCards)) {
                return true;
            }
        }
    }
    return false;
};

/**是否要得起 主要是判断自己是否要得起
 *_lastCardInfo:上家出的牌
 * _currChoiceCardsInfo：当前选中的牌
 */
AIGetCard.isOK = function(_lastCardInfo, _currChoiceCardsInfo) {
    var cardCnt = _.size(_currChoiceCardsInfo)
    var cardGroupType = _lastCardInfo.type;
    if (EumPkGroup.ONE == cardGroupType) {
        if (1 == cardCnt) {
            var lastCardValue = _lastCardInfo.cardsInfo[0].value;
            var choiceCardValue = _currChoiceCardsInfo[0].value;
            return lastCardValue < choiceCardValue;
        }
    } else if (EumPkGroup.PAIRS == cardGroupType) {
        if (2 == cardCnt) {
            var lastCardValue = _lastCardInfo.cardsInfo[0].value;
            var choiceCardValue = _currChoiceCardsInfo[0].value;
            return lastCardValue < choiceCardValue;
        }
    } else if (EumPkGroup.THREE == cardGroupType) {
        if (3 == cardCnt) {
            var isThree = (_currChoiceCardsInfo[0].value == _currChoiceCardsInfo[1].value) && (_currChoiceCardsInfo[0].value == _currChoiceCardsInfo[2].value)
            var lastCardValue = _lastCardInfo.cardsInfo[0].value;
            var choiceCardValue = _currChoiceCardsInfo[0].value;
            return true == isThree && lastCardValue < choiceCardValue;
        }
    } else if (EumPkGroup.THREEONE == cardGroupType) {
        if (4 == cardCnt) {
            var sameCardsList = FindSameCardValueTool.check(_currChoiceCardsInfo);
            sameCardsList = _.filter(sameCardsList, function(sameCards) {
                return _.size(sameCards) == 3;
            });
            if (_.size(sameCardsList) == 1) {
                sameCardsList = _.flatten(sameCardsList);
                //获取上加三个一样的牌值
                var lastSameCardsList = FindSameCardValueTool.check(_lastCardInfo.cardsInfo);
                lastSameCardsList = _.filter(lastSameCardsList, function(sameCards) {
                    return _.size(sameCards) == 3;
                });
                lastSameCardsList = _.flatten(lastSameCardsList);
                return lastSameCardsList[0].value < sameCardsList[0].value;
            }
        }
    } else if (EumPkGroup.THREEPAIRS == cardGroupType) {
        if (5 == cardCnt) {
            var sameCardsList = FindSameCardValueTool.check(_currChoiceCardsInfo);
            var sameThreeCardsList = _.filter(sameCardsList, function(sameCards) {
                return _.size(sameCards) == 3;
            });
            var sameTwoCardsList = _.filter(sameCardsList, function(sameCards) {
                return _.size(sameCards) == 2;
            });
            if (1 == _.size(sameThreeCardsList) && 1 == _.size(sameTwoCardsList)) {
                sameThreeCardsList = _.flatten(sameThreeCardsList);
                //获取上加三个一样的牌值
                var lastSameCardsList = FindSameCardValueTool.check(_lastCardInfo.cardsInfo);
                lastSameCardsList = _.filter(lastSameCardsList, function(sameCards) {
                    return _.size(sameCards) == 3;
                });
                lastSameCardsList = _.flatten(lastSameCardsList);
                return lastSameCardsList[0].value < sameThreeCardsList[0].value;
            }
        }
    } else if (EumPkGroup.STRAIGHT == cardGroupType) {
        if (cardCnt == _.size(_lastCardInfo.cardsInfo)) {
            var _isOk = FindShunZiTool.isShunZi(_currChoiceCardsInfo, cardCnt);
            if (_isOk) {
                var lastMaxCardInfo = _.max(_lastCardInfo.cardsInfo, function(_cardInfo) {
                    return _cardInfo.value
                });
                var currMaxCardInfo = _.max(_currChoiceCardsInfo, function(_cardInfo) {
                    return _cardInfo.value
                });
                return lastMaxCardInfo.value < currMaxCardInfo.value;
            }
        }
    } else if (EumPkGroup.BOMB == cardGroupType) {
        if (4 == cardCnt) {
            var sameCardsList = FindSameCardValueTool.check(_currChoiceCardsInfo);
            if (1 == _.size(sameCardsList)) {
                sameCardsList = _.flatten(sameCardsList);
                if (4 == _.size(sameCardsList)) {
                    var lastCardValue = _lastCardInfo.cardsInfo[0].value;
                    var choiceCardValue = sameCardsList[0].value;
                    return lastCardValue < choiceCardValue;
                }
            }
        }
    }
    return false;
};

/***AI出牌
 * _cardIds:[1,23,3];
 * _lastCardInfo:{type:1,cardIds:[1,2,3,4]}
 * return  {
                type: findType,//类型
                result: [{cardId:1,value:1}]//要的起得牌
            }
 */
AIGetCard.doAI = function(_cardIds, _lastCardInfo) {
    var _type = _lastCardInfo.type;
    /**如果别家出的是王炸必现要不起 */
    if (EumPkGroup.BOMBKING == _type) {
        return {
            isFind: false,
        }
    }
    var _cardsInfo = PkCardTool.getCardInfos(_cardIds);
    _cardsInfo = _.sortBy(_cardsInfo, function(_cardInfo) {
        return -_cardInfo.value;
    });
    _lastCardInfo.cardsInfo = PkCardTool.getCardInfos(_lastCardInfo.cardIds);

    var types = this.getCanGetTypes(_type);
    var typeSize = _.size(types);
    for (var i = 0; i < typeSize; ++i) {
        var findType = types[i];
        if (EumPkGroup.ONE == findType) {
            var _result = AIGetOne.do(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _result.type = findType;
                return _result;
            }
        } else if (EumPkGroup.PAIRS == findType) {
            var _result = AIGetPairs.do(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _result.type = findType;
                return _result;
            }
        } else if (EumPkGroup.THREE == findType) {
            var _result = AIGetThree.do(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _result.type = findType;
                return _result;
            }
        } else if (EumPkGroup.THREEONE == findType) {
            var _result = AIGetThree.doBeltOne(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _result.type = findType;
                return _result;
            }
        } else if (EumPkGroup.THREEPAIRS == findType) {
            var _result = AIGetThree.doBeltPairs(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _result.type = findType;
                return _result;
            }
        } else if (EumPkGroup.STRAIGHT == findType) {
            var _result = AIGetShunZi.do(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _result.type = findType;
                return _result;
            }
        } else if (EumPkGroup.BOMB == findType) {
            var _result = AIGetBomb.do(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _result.type = findType;
                return _result;
            }
        } else if (EumPkGroup.BOMBKING == findType) {
            var _result = AIGetBombKing.do(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _result.type = findType;
                return _result;
            }
        }
    }
    return {
        isFind: false,
    }
};

/***要得起的牌型
 * _type:
 */
AIGetCard.getCanGetTypes = function(_type) {
    return [_type, EumPkGroup.BOMB, EumPkGroup.BOMBKING];
};